package com.example.myapplicationndk.translate;

import android.os.Bundle;
import android.view.View;

import com.example.myapplicationndk.databinding.ActivityTranslateMainBinding;
import com.example.myapplicationndk.utils.Logd;
import com.google.gson.Gson;

import java.util.Map;

import androidx.appcompat.app.AppCompatActivity;

public class TranslateMainActivity extends AppCompatActivity {

    ActivityTranslateMainBinding mBinding;
    private String TAG = this.getClass().getName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mBinding = ActivityTranslateMainBinding.inflate(getLayoutInflater());
        setContentView(mBinding.getRoot());


        mBinding.etInput.setText(WebITS.TEXT);

        mBinding.btnTranslate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                buildRequest();
            }
        });
    }

    private void buildRequest() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String body = null;
                try {
                    mBinding.tvResult.setText("正在翻译......");
                    String text = mBinding.etInput.getText().toString();
                    body = WebITS.buildHttpBody(text);
                    //System.out.println("【ITSWebAPI body】\n" + body);
                    Map<String, String> header = WebITS.buildHttpHeader(body);
                    Map<String, Object> resultMap = HttpUtil.doPost2(WebITS.WebITS_URL, header, body);
                    if (resultMap != null) {
                        String resultStr = resultMap.get("body").toString();
                        System.out.println("【ITS WebAPI 接口调用结果】\n" + resultStr);
                        //以下仅用于调试
                        WebITS.ResponseData resultData = new Gson().fromJson(resultStr, WebITS.ResponseData.class);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    Logd.Logd(resultData.toString());
                                    mBinding.tvResult.setText(resultData.getData().getResult().getTrans_result().getDst().toString());
                                } catch (Exception e) {
                                    Logd.Logd(e.toString());
                                }
                            }
                        });
                        int code = resultData.getCode();
                        if (resultData.getCode() != 0) {
                            System.out.println("请前往https://www.xfyun.cn/document/error-code?code=" + code + "查询解决办法");
                        }
                    } else {
                        Logd.Logd("resultMap is null");
                        System.out.println("调用失败！请根据错误信息检查代码，接口文档：https://www.xfyun.cn/doc/nlp/xftrans/API.html");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }).start();

    }

}